﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using DiscoveryLogic.Analytics.BusinessLogic.Entities;
using DiscoveryLogic.Analytics.DataObjects;
using DiscoveryLogic.Analytics.DataObjects.Rules;
using DiscoveryLogic.Common.Utilities;

namespace DiscoveryLogic.Analytics.BusinessLogic.Importers
{
    public class DataImportWorker
    {
        #region fields
        private EntityImportRule _Rule;
        private string _DataSetName;
        private string _IconPath;
        #endregion

        public DataImportWorker(string setName, EntityImportRule rule, string iconPath)
        {
            this._DataSetName = setName;
            this._Rule = rule;
            this._IconPath = iconPath;
        }

        public EntityDataSet ImportData()
        {
            EntityDataSet entitySet=new EntityDataSet(this._DataSetName, this._IconPath);
            SetDataProvider provider=new SetDataProvider();
            provider.DataSourcePath = this._Rule.DataSourcePath;
            provider.FieldPaths = this._Rule.EntityFieldSrcFieldMappings;
            EntityBase tgtEntity =(EntityBase) Activator.CreateInstance(EntityTypeUtil.GetType(this._Rule.TgtEntityType));
            provider.KeyFieldName = tgtEntity.IDField;
            provider.LabelField = tgtEntity.NameField;
            provider.RecordListPath = this._Rule.RecordListPath;
            provider.SetName = this._DataSetName;
            provider.SourceType = this._Rule.SrcDataFormat;
            DataTable dt = provider.GetDataFromProvider();
            entitySet.Entities = EntityManager.ToEntityList(this._Rule.TgtEntityType, dt,
                                                            this._Rule.EntityFieldSrcFieldMappings);
            entitySet.EntityType = this._Rule.TgtEntityType;
            entitySet.IconPath = this._IconPath;
            entitySet.SourceType = this._Rule.SrcDataFormat;
            return entitySet;
        }
    }
}
